﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Actuals.CoreDomain.Business;
using Actuals.CoreDomain;
using Actuals.CoreDomain.Data;
using Actuals.Data;

namespace Actuals.Business
{
    public class BusinessDashBoard : IBusinessDashBoard
    {

        private IBusinessTask _businessTask;
        private IBusinessActual _businessActual;
        private IBusinessRelease _businessRelease;
        private IBusinessRemainingTime _businessRemainingTime;

        public BusinessDashBoard(IBusinessTask businessTask, IBusinessActual businessActual,
            IBusinessRelease businessRelease, IBusinessRemainingTime businessRemainingTime)
        {
            _businessTask = businessTask;
            _businessActual = businessActual;
            _businessRelease = businessRelease;
            _businessRemainingTime = businessRemainingTime;
        }

        public BusinessDashBoard()
        {
            _businessTask = new BusinessTask();
            _businessActual = new BusinessActual();
            _businessRelease = new BusinessRelease();
            _businessRemainingTime = new BusinessRemainingTime();

        }

        #region IBusinessDashBoard Members

        public IDashBoard GetDashBoard(int projectId, int releaseId)
        {

            IDashBoard dashBoard = new DashBoard();
            IList<ITask> tasks = _businessTask.GetDashBoardTasks(projectId, releaseId);
            IRelease release = _businessRelease.GetRelease(projectId, releaseId);
            IList<IActual> actualList = _businessActual.GetActualsOnlyForDevelopers(release.StartDate, release.EndDate, projectId, releaseId);
            int _totalElapsed = 0;
            foreach (ITask task in tasks)
            {
                _totalElapsed = GetElapsedTimeForTask(task.TaskId, actualList);
                task.Elapsed = _totalElapsed;
                task.Remaining = task.CurrentEstimation - _totalElapsed;
            }
            dashBoard.Tasks = tasks;
            dashBoard = PopulateTotals(dashBoard);
            dashBoard.TotalRemainingForRelease = _businessRemainingTime.GetReleaseRemainingTime(projectId, releaseId);
            dashBoard.TotalRemainingToFinish = dashBoard.TotalRemainingForRelease - dashBoard.TotalRemainingForTasks;
            return dashBoard;

        }

        public IDashBoard GetDashBoardAsOf(int projectId, int releaseId, DateTime asOfdate)
        {
            IDashBoard dashBoard = new DashBoard();
            IList<ITask> tasks = _businessTask.GetDashBoardTasks(projectId, releaseId);
            IRelease release = _businessRelease.GetRelease(projectId, releaseId);
            IList<IActual> actualList = _businessActual.GetActualsOnlyForDevelopers(release.StartDate, asOfdate, projectId, releaseId);
            int _totalElapsed = 0;
            foreach (ITask task in tasks)
            {
                _totalElapsed = GetElapsedTimeForTask(task.TaskId, actualList);
                task.Elapsed = _totalElapsed;
                task.Remaining = task.CurrentEstimation - _totalElapsed;
            }
            dashBoard.Tasks = tasks;
            dashBoard = PopulateTotals(dashBoard);
            dashBoard.TotalRemainingForRelease = _businessRemainingTime.GetReleaseRemainingTimeAsOfDate(projectId, releaseId, asOfdate);
            dashBoard.TotalRemainingToFinish = dashBoard.TotalRemainingForRelease - dashBoard.TotalRemainingForTasks;

            IList<ITask> _tasks = dashBoard.Tasks.OrderBy(x => x.TaskType).ToList();
            dashBoard.Tasks = _tasks;

            return dashBoard;
        }

        public int GetElapsedTimeForTask(int taskId, IList<IActual> actualList)
        {
            return actualList.Where(x => x.TaskId == taskId).Sum(x => x.NoOfHours);
        }

        public IDashBoard PopulateTotals(IDashBoard dashBoard)
        {
            if (dashBoard.Tasks != null)
            {
                IList<ITask> tasks = dashBoard.Tasks;
                dashBoard.TotalInitialEstimation = tasks.Sum(x => x.InitialEstimation);
                dashBoard.TotalCurrentEstimation = tasks.Sum(x => x.CurrentEstimation);
                dashBoard.TotalElapsed = tasks.Sum(x => x.Elapsed);
                dashBoard.TotalRemainingForTasks = tasks.Sum(x => x.Remaining);

            }
            else
            {

            }

            return dashBoard;

        }


        #endregion
    }
}
